package com.example.crm.service;

import com.example.crm.entity.UserLogin;
import com.example.crm.repository.UserLoginRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserLoginService {
    @Autowired
    private UserLoginRepository userLoginRepository;

    public UserLogin register(UserLogin user) {
        // 检查用户名是否已存在
        if (userLoginRepository.findByUsername(user.getUsername()).isPresent()) {
            throw new RuntimeException("用户名已存在");
        }
        // 还可以加点密码加密啥啥的
        return userLoginRepository.save(user);
    }

    public UserLogin login(String username, String password) {
        UserLogin user = userLoginRepository.findByUsername(username)
                .orElseThrow(() -> new RuntimeException("用户不存在"));
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }
        return user;
    }
}